package com.tools.controller;

import com.tools.model.ImgZipModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import org.apache.log4j.Logger;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * ImgZipController
 *
 * @author < a href=" ">ZhangYang</ a>
 * @version v1.0.01
 * @date 2018-08-06 11:06:04
 */

public class ImgZipController extends BaseController {
  private static final Logger LOG = Logger.getLogger(ImgZipController.class);

  @FXML
  private AnchorPane main;
  @FXML
  private TableView<ImgZipModel> imgList;
  @FXML
  private TableColumn<ImgZipModel, String> imgColumn;
  @FXML
  private TableColumn<ImgZipModel, String> nameColumn;
  @FXML
  private TableColumn<ImgZipModel, String> sizeColumn;

  private ObservableList<ImgZipModel> data = FXCollections.observableArrayList();

  @Override
  public void initialize(URL location, ResourceBundle resources) {
    imgColumn.setCellValueFactory(cellData -> cellData.getValue().imgUrlProperty());
    nameColumn.setCellValueFactory(cellData -> cellData.getValue().imgNameProperty());
    sizeColumn.setCellValueFactory(cellData -> cellData.getValue().imgSizeProperty());
  }

  /**
   * 选择图片
   *
   * @param event
   */
  public void onSelectProjectPath(ActionEvent event) {
    FileChooser fileChooser = new FileChooser();
    fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("图片", "*.jpg", "*.png", "*.gif"));
    List<File> file = fileChooser.showOpenMultipleDialog(this.getPrimaryStage());
    if (file != null) {
      for (File f : file) {
        Image image = new Image(f.getAbsoluteFile().toURI().toString(), 100, 100, false, false);
        data.add(new ImgZipModel(f.getAbsoluteFile().toURI().toString(), f.getName(), f.length() + ""));
      }

      imgList.setItems(data);
    }
  }
}
